package industry_2024.industry_08.indicator

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.col

import java.util.Properties

object indicator03 {
  def main(args: Array[String]): Unit = {
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("指标计算第三题")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
//      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
//      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()

    spark.table("dwd08.fact_change_record")
      .filter(col("changeendtime").isNotNull)
      .createOrReplaceTempView("data")

    val result=spark.sql(
      """
        |select distinct
        |changemachineid as machine_id,
        |changerecordstate as change_record_state,
        |sum(unix_timestamp(changeendtime) - unix_timestamp(changestarttime))
        |over(partition by Year(changestarttime),Month(changestarttime),changemachineid,changerecordstate) as duration_time,
        |Year(changestarttime) as year,
        |Month(changestarttime) as month
        |from data
        |""".stripMargin)

      val connect=new Properties()
      connect.setProperty("user","root")
      connect.setProperty("password","123456")
    connect.setProperty("driver","com.mysql.jdbc.Driver")

    result.write.mode("overwrite")
      .jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false","machine_state_time08",connect)


    spark.close()
  }

}
